package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKLiquidation;
import controllers.Logs;

public class BookKeepersLiquidation extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private DropDownChoice officer;
	private Model dateModel = new Model();
	private Model lfModel = new Model();
	private Model rcvModel = new Model();
	private Model officerModel = new Model();
	private Model actualAmountModel = new Model();
	private Model amountAdvancedModel = new Model();
	private Model amountReturnedModel = new Model();
	private List choice = Arrays.asList(new String[] { "President", "Secretary", "Treasurer" });

	public BookKeepersLiquidation() throws SQLException, IOException
	{
		TextField date = new TextField("date", dateModel);
		date.setRequired(true);
		
		TextField lfNo = new TextField("lfNo", lfModel);
		lfNo.setRequired(true);
		
		TextField rcv = new TextField("rcv", rcvModel);
		rcv.setRequired(true);
		
		officer = new DropDownChoice("officer", officerModel, choice);
		
		TextField actualAmount = new TextField("actualAmount", actualAmountModel);
		actualAmount.setRequired(true);
		
		TextField amountAdvanced = new TextField("amountAdvanced", amountAdvancedModel);
		amountAdvanced.setRequired(true);
		
		TextField amountReturned = new TextField("amountReturned", amountReturnedModel);
		amountReturned.setRequired(true);
		
		Form form = new Form("LiquidationRegisterForm");
		
		form.add(new Button("add"){
		
		private static final long serialVersionUID = 1L;

		public void onSubmit(){
			
			try {
				String date = (String) dateModel.getObject();
				String rcv = (String) rcvModel.getObject();
				String lfNo = (String) lfModel.getObject();
				String officer = (String) officerModel.getObject();
				String actualAmount = (String) actualAmountModel.getObject();
				String amountAdvanced = (String) amountAdvancedModel.getObject();
				String amountReturned = (String) amountReturnedModel.getObject();
				
				String uploader = ((Sessions)getSession()).getDesc();
				
				Date today = new Date();
				Timestamp timestamp = new Timestamp(today.getTime());
				String ts = timestamp.toString();
				
				
				
				BKLiquidation bl = new BKLiquidation();
				bl.setDate(date);
				bl.setLf(lfNo);
				bl.setRcv(rcv);
				int newActualAmount = Integer.parseInt(actualAmount);
				bl.setActualAmount(newActualAmount);
				int newAmountAdvanced = Integer.parseInt(actualAmount);
				bl.setAmountAdvanced(newAmountAdvanced);
				int newAmountReturned = Integer.parseInt(amountReturned);
				bl.setAmountReturned(newAmountReturned);
				bl.setOfficer(officer);		
				bl.setUploader(uploader);
				bl.setDate_created(ts);
				bl.setStatus("PENDING");
				String newId = Integer.toString(bl.getBkl_id());
				
				System.out.println("date: " + date + " rcv: " + rcv + " lf: " + lfNo + " officer: " + officer + " uploader: " + uploader + " uploader: " + uploader
						+ "date created" + ts + "actual" + actualAmount + "advanced" + amountAdvanced + "returned" 
						+ amountReturned + "id" + newId);
				
				
				sqlMap.insert("BKLiquidation.insert", bl);
				
				Logs log = new Logs();
				log.setLog_activity("Book Keepers Liquidation Register" + " " + bl.getLf());
				log.setLog_officer(uploader);
				log.setLog_time(ts);
				
				sqlMap.insert("Logs.insert", log);
			
				BookKeepersLiquidationList page = new BookKeepersLiquidationList();
				setResponsePage(page);
				error("added");
		}
			catch (SQLException e){
				 error("Fill up necessary fields");
				 e.printStackTrace();
			  } catch (IOException e) {
				e.printStackTrace();
			}
		}});
		
		add(new BookmarkablePageLink("manageForms", ManageForms.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("view", BookKeepersLiquidationList.class));
		
		
		add(new FeedbackPanel("error"));
		
		form.add(date);
		form.add(rcv);
		form.add(officer);
		form.add(lfNo);
		form.add(amountAdvanced);
		form.add(actualAmount);
		form.add(amountReturned);
		add(form);
		
		
	}
	
	
}
